package com.wisdudu.blank.app;

import io.realm.DynamicRealm;
import io.realm.FieldAttribute;
import io.realm.RealmMigration;
import io.realm.RealmSchema;

/**
 * Created by qyf on 2016/8/31.
 * 数据库升级配置文件
 */

public class RealmVersionUpgradeSetting implements RealmMigration {
    public static int version =50;//数据库版本，手动加1

    @Override
    public void migrate(DynamicRealm realm, long oldVersion, long newVersion) {
        RealmSchema schema = realm.getSchema();
        //数据库新增Brand类
        if (oldVersion == 42) {
            schema.create("Brand")
                    .addField("brandid", String.class)
                    .addField("brandname", String.class)
                    .addField("brandurl", String.class)
                    .addField("intypeid", String.class, FieldAttribute.PRIMARY_KEY)
                    .addField("rowcount", String.class);
            oldVersion++;
        }
        //更改key
        if (oldVersion == 43) {
            schema.get("Brand")
                    .removePrimaryKey()
                    .addPrimaryKey("brandid");
            oldVersion++;
        }
        //removekey
        if (oldVersion == 44) {
            schema.get("Brand")
                    .removePrimaryKey();
            oldVersion++;
        }
        if (oldVersion == 45) {
            schema.get("Brand")
                    .addPrimaryKey("brandid");
            oldVersion++;
        }
        //添加pinyin字段
        if (oldVersion == 46) {
            schema.get("Brand")
                    .addField("pinyin", String.class);
            oldVersion++;
        }
        if (oldVersion == 48) {
            schema.get("UserInfo")
                    .addField("voiceprint", String.class);
            oldVersion++;
        }
        if (oldVersion == 49) {
            schema.get("UserInfo")
                    .addField("houseid", String.class);
            schema.get("UserInfo")
                    .addField("housename", String.class);
            oldVersion++;
        }
    }
}
